﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Bll
{
    class CUserBasicManage
    {
        /// <summary>
        /// 纯粹是创建一个新的对象
        /// </summary>
        /// <returns></returns>
       public static CUserBasic CreateBasicUser()
       {
           return   new CUserBasic();
       }
        /// <summary>
        /// 将对象保存到数据库中去
        /// </summary>
        /// <param name="c">用户对象</param>
        /// <returns>成功返回保存的对象，否则为空</returns>
       public static CUserBasic Save(CUserBasic c)
       {
           try
           {
               int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_AddUserBasic",
                   c.ID, c.name, c.password, c.email, c.phone, c.address, c.postcode, c.rankID, c.firstName, c.lastName ,c.country);
             
               if(0==num)
                   return null;
               return c;
           }
           catch (Exception e)
           {
               return null;
           } 
       }
       /// <summary>
       /// 将修改后对象保存到数据库中去
       /// </summary>
       /// <param name="c">是否成功</param>
       /// <returns>成功返回真，否则假</returns>
       public static bool Update(CUserBasic c)
       {
           try
           {
               int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_SaveUserBasic", c.ID, c.name, c.password, c.phone, c.email, c.address, c.postcode, c.rankID, c.firstName, c.lastName);
               if (0 == num)
                   return false;
               return true;
           }
           catch (Exception e)
           {
               return false ;
           }
       }
       /// <summary>
       /// 删除对象
       /// </summary>
       /// <param name="c">是否成功</param>
       /// <returns>成功返回真，否则假</returns>
       public static bool Delete(CUserBasic c)
       {
           try
           {
               int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_RemoveUserBasic", c.ID);
               if (0 == num)
                   return false;
               return true;
           }
           catch (Exception e)
           {
               return false;
           }
       
       
       }
      
     
        /// <summary>
        /// 通过id找那个用户
        /// </summary>
        /// <param name="ID">用户id</param>
        /// <returns>用户对象</returns>
       public static CUserBasic GetBasicUser(String ID)
       {
           SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetUserBasic", ID);
           try
           {  
               
               if (reader.Read())
               { 
                   CUserBasic c=new CUserBasic();

                   c.address=reader["address"].ToString();
                   c.email=reader["email"].ToString();
                   c.name=reader["name"].ToString();
                   c.password=reader["password"].ToString();
                   c.phone=reader["phone"].ToString();
                   c.postcode = reader["postcode"].ToString();
                   c.rankID = (int)reader["rankID"];
                   c.firstName = reader["firstName"].ToString();
                   c.lastName = reader["lastName"].ToString();

                   reader.Close();
                   return c;
               }
               return null;
           }
           catch (Exception e)
           {
               //reader.Close();
               return null;
           }
           finally
           {
               reader.Close();
           }

       }

       /// <summary>
       /// 通过name找那个用户
       /// </summary>
       /// <param name="name">用户 登录名 注册的时候使用</param>
       /// <returns>用户对象</returns>
       public static CUserBasic GetBasicUserByName(String name)
       {
           SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetUserBasicByName" , name);
           try
           {
               
               if (reader.Read())
               {
                   CUserBasic c = new CUserBasic();
                   c.ID = reader["ID"].ToString();
                   c.address = reader["address"].ToString();
                   c.email = reader["email"].ToString();
                   c.name = reader["name"].ToString();
                   c.password = reader["password"].ToString();
                   c.phone = reader["phone"].ToString();
                   c.postcode = reader["postcode"].ToString();
                   c.rankID = (int)reader["rankID"];
                   c.firstName = reader["firstName"].ToString();
                   c.lastName = reader["lastName"].ToString();

                   reader.Close();
                   return c;
               }
               return null;
           }
           catch (Exception e)
           {
               //reader.Close();
               return null;
           }
           finally
           {
               reader.Close();
           }

       }

        /// <summary>
        /// 通过condition的条件找对象，condition 可以这样写and (a=1 or b=4)
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>一个集合</returns>
       public List<CUserBasic> GetUserByCondition(String condition)
       {
           List<CUserBasic> users = new List<CUserBasic>();
           SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "select id,address,email,name,password,phone,posetcode,rankID from UserBasic where 1=1 " + condition);
           try
           {
               
               while (reader.Read())
               {
                   CUserBasic c = new CUserBasic();
                   c.ID=reader["id"].ToString();
                   c.address=reader["address"].ToString();
                   c.email=reader["email"].ToString();
                   c.name=reader["name"].ToString();
                   c.password=reader["password"].ToString();
                   c.phone=reader["phone"].ToString();
                   c.postcode = reader["postcode"].ToString();
                   c.rankID = (int)reader["rankID"];
                   c.firstName = reader["firstName"].ToString();
                   c.lastName = reader["lastName"].ToString();
                   users.Add(c);
               }
               
               return users;
           }
           catch (Exception e)
           {

               //reader.Close();
               return null;
           }
           finally
           {
               reader.Close();
           }
       }
    }
}
